from django.http import JsonResponse
class ResFormat:

    SUCCESS = 0,'成功'
    PARAM_ERROR = 4001,'参数不存在'
    PARAMS_ERROR = 4008,'参数错误'
    UNKNOWN_ERROR = 4100,'未知错误'
    ACCESS_RESTRICTION = 4002,'访问限制'
    ACCOUNT_REGISTER = 4003,'账户已注册'
    CODE_ERROR = 4004,'验证码错误'
    CODE_EXPIRED = 4005,'验证码失效'
    PASSWORD_ERROR = 4006,'密码错误'
    LOGIN_OUTTIME = 4007,'登录状态失效，请重新登录'
    UPLOAD_EXCEPTION = 4009,'图片上传异常'

    def __init__(self,status='SUCCESS',data=''):
        if hasattr(self,status):
            status = getattr(self,status)
        else:
            status = self.UNKNOWN_ERROR

        self.code,self.message = status
        self.data = data

    def res(self):
        dic = {'code':self.code}
        if self.message:
            dic['message'] = self.message
        if self.data:
            dic['data'] = self.data
        return dic

    def json_response(self):
        return JsonResponse(self.res(),json_dumps_params={'ensure_ascii':False})